package com.frogs.ucenter.v1.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.frogs.framework.util.MapUtil;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.StringUtil;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.ServiceOperationException;
import com.frogs.ucenter.dao.UserDrugDao;
import com.frogs.ucenter.entity.UserDrug;
import com.frogs.ucenter.v1.request.UserDrugListRequest;
import com.frogs.ucenter.v1.request.UserDrugPartialListRequest;
import com.frogs.ucenter.v1.request.UserDrugShowRequest;
import com.frogs.ucenter.v1.response.PageListResponse;
import com.frogs.ucenter.v1.response.ShowResponse;
import com.frogs.ucenter.v1.service.UserDrugService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
@Service(ApiConstants.VERSION + "userDrugService")
public class UserDrugServiceImpl implements UserDrugService {

	Logger log = LoggerFactory.getLogger(UserDrugServiceImpl.class);

	@Resource
	private UserDrugDao userDrugDao;

	@Override
	public Response listDrugs(Request request) throws Exception {
		PageListResponse<Map<String,Object>> response = new PageListResponse<Map<String,Object>>();
		UserDrugListRequest udreq = (UserDrugListRequest) request;
		PageList<Map<String,Object>> drugs = new PageList<Map<String,Object>>();
		
		try {
			int pageNo = udreq.getPageNum();
			int pageSize = udreq.getPageSize();
			Map<String,Object> params = new HashMap<String,Object>();
			
			if(udreq.getType() != -1) {
				params.put("eqType", udreq.getType());
			}
			
			if(udreq.getCategory() != -1) {
				params.put("eqCategory", udreq.getCategory());
			}
			
			drugs = userDrugDao.selectByMapWithMap(params, pageNo, pageSize);
			
			response.setList(MapUtil.convertToList(MapUtil.groupList(drugs, "typeName")));
			response.setPageTurn(drugs.getPageTurn());
			response.setTip("获取药物列表成功!");
			return response;
			
		} catch(Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
	}

	@Override
	public Response showDrugs(Request request) throws Exception {
		UserDrugShowRequest udsreq = (UserDrugShowRequest)request;
		ShowResponse<UserDrug> response = new ShowResponse<UserDrug>();
		try {
			UserDrug drug = userDrugDao.selectById(udsreq.getId());
			response.setInfo(drug);
			response.setTip("查询药物成功");
		} catch(Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}

	@Override
	public Response listDrugsPartlist(Request request) throws Exception {
		PageListResponse<UserDrug> response = new PageListResponse<UserDrug>();
		PageList<UserDrug> drugs = new PageList<UserDrug>();
		UserDrugPartialListRequest partListReq = (UserDrugPartialListRequest)request;
		
		try {
			Map<String,Object> params = new HashMap<String,Object>();
			String ids = partListReq.getIds();
			if(!StringUtil.isEmpty(ids)) {
				params.put("inIdList", ids.split(","));
			}
			drugs = userDrugDao.selectByMap(params, false);
			response.setList(drugs);
			response.setPageTurn(drugs.getPageTurn());
		} catch(Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}


}